/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.feldschmid.subdroid;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.feldschmid.subdroid.db.RepositoryDbAdapter;

public class RepositoryEdit extends Activity {

	private Long mRowId;
	private EditText mNameText;
	private EditText mURLText;
	private EditText mUserText;
	private EditText mPassText;
	private EditText mIntervalText;
	private CheckBox mNotificationBox;
	private RepositoryDbAdapter mDbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repository_edit);

		mDbHelper = new RepositoryDbAdapter(this);
		mDbHelper.open();

		mNameText = (EditText) findViewById(R.id.name);
		mURLText = (EditText) findViewById(R.id.url);
		mUserText = (EditText) findViewById(R.id.user);
		mPassText = (EditText) findViewById(R.id.pass);
		mIntervalText = (EditText) findViewById(R.id.interval);
		mNotificationBox = (CheckBox) findViewById(R.id.notification);

		Button confirmButton = (Button) findViewById(R.id.confirm);

		mRowId = savedInstanceState != null ? savedInstanceState
				.getLong(RepositoryDbAdapter.KEY_ROWID) : null;
		if (mRowId == null) {
			Bundle extras = getIntent().getExtras();
			mRowId = extras != null ? extras
					.getLong(RepositoryDbAdapter.KEY_ROWID) : null;
		}

		populateFields();

		confirmButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				setResult(RESULT_OK);
				finish();
			}

		});
	}

	private void populateFields() {
		if (mRowId != null) {
			Cursor repo = mDbHelper.fetchRepository(mRowId);
			startManagingCursor(repo);
			mNameText.setText(repo.getString(repo
					.getColumnIndexOrThrow(RepositoryDbAdapter.KEY_NAME)));
			mUserText.setText(repo.getString(repo
					.getColumnIndexOrThrow(RepositoryDbAdapter.KEY_URL)));
			mNotificationBox.setChecked(Boolean.valueOf(repo.getString(repo
					.getColumnIndexOrThrow(RepositoryDbAdapter.KEY_NOTIFICATION))));
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong(RepositoryDbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void saveState() {
		String name = mNameText.getText().toString();
		String url = mURLText.getText().toString();
		String user = mUserText.getText().toString();
		String pass = mPassText.getText().toString();
		String interval = mIntervalText.getText().toString();
		String notification = String.valueOf(mNotificationBox.isChecked());
		
		if(name==null || name.equals("")) {
			name = "Please enter a Name";
		}

		if (mRowId == null) {
			long id = mDbHelper.createRepository(name, url, user, pass,
					interval, notification);
			if (id > 0) {
				mRowId = id;
			}
		} else {
			mDbHelper.updateRepository(mRowId, name, url, user, pass, interval,
					notification);
		}
	}
}
